package com.dbkj.axb_relation;

import com.dbkj.common.model.AxbRelation;
import com.dbkj.common.utils.RedisUtils;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

/**
 * @Author: Max
 * @Date: 2019/10/29 11:07
 */

public class AxbRelationService {

    private static final AxbRelation dao = new AxbRelation().dao();

    public Page<AxbRelation> paginate(Integer pageNumber, Integer pageSize,String name) {
        SqlPara sql = dao.getSqlPara("relation.show", Kv.by("name",name));
        return dao.paginate(pageNumber,pageSize,sql);
    }

    public void delete(Integer id){
        AxbRelation a = dao.findById(id);
        a.delete();
        if(PropKit.getBoolean("is_cluster")){
            RedisUtils.cluster.del("tela_"+a.getARelation()+"_concurrent_list");
        }else{
            RedisUtils.jedis.del("tela_"+a.getARelation()+"_concurrent_list");
        }
    }

    public Integer getIdByName(String name){
        AxbRelation a = dao.findFirst("select * from axb_relation where a_relation ='"+name+"'");
        if(null!=a){
            return a.getId();
        }else {
            return null;
        }

    }

    public AxbRelation getById(Integer relationId) {
        return dao.findById(relationId);
    }


    public void checkFlag(Integer id,Boolean check) {
        if(check){
            Db.update("update axb_relation set check_flag = 1 where id =?",id);
        }else{
            Db.update("update axb_relation set check_flag = 0 where id =?",id);
        }

    }
}
